import { View } from "@tarojs/components";
import { useState } from "react";
import { useRouter, useDidShow } from "@tarojs/taro";

import { AtListItem } from "taro-ui";

import { getPointDetailApi, getProjectInfoApi } from "../../utils/api";
import { formatTime2 } from "../../utils/to";

import "./index.scss";

// 设备类型 1-半自动；2-全自动;3-机械式。
const deviceObj = {
  1: "半自动",
  2: "全自动",
  3: "机械式"
};

// 1-未知；2-正常；3-蚁情；4-入侵；5-低电量
// 报警类型
const warnObj = {
  1: "无",
  2: "正常",
  3: "蚁情",
  4: "入侵",
  5: "低电量",
  9: "其他"
};

// 检查状态 1-正常；2-发现蚁情；3-发现蚁情但量不多，需再观察；4-发现蚁情但已撤离，无需灭杀；5-装置异常；6-装置被入侵破坏；7-装置丢失；8-装置被移位；9-其他
const inspectStateObj = {
  0: "无",
  1: "正常",
  2: "发现蚁情",
  3: "发现蚁情但量不多，需再观察",
  4: "发现蚁情但已撤离，无需灭杀",
  5: "装置异常",
  6: "装置被入侵破坏",
  7: "装置丢失",
  8: "装置被移位",
  9: "其他"
};

// 措施类型 1-无操作；2-简单恢复；3-替换电池；4-替换装置；5-灭蚁；6-推迟灭蚁(再观察)；9-其他
const inspectMeasureObj = {
  0: "无",
  1: "无操作",
  2: "简单恢复",
  3: "替换电池",
  4: "替换装置",
  5: "灭蚁",
  6: "推迟灭蚁(再观察)",
  9: "其他"
};

const Index = () => {
  const router = useRouter();
  const [point_info, set_point_info] = useState({});

  useDidShow(() => {
    getInfo();
  });

  const getInfo = async () => {
    const res = await getPointDetailApi({
      point_id: router.params.id
    });
    if (res.res === 0) {
      set_point_info(res.record);
    } else {
      Taro.atMessage({
        message: res.msg,
        type: "warning"
      });
    }
  };

  return (
    <View>
      <AtListItem title="所属工程" extraText={point_info.project_name} />
      <AtListItem title="布点名称" extraText={point_info.name} />
      <AtListItem
        title="绑定设备"
        extraText={
          point_info.device_type ? deviceObj[point_info.device_type] : "未绑定"
        }
      />
      <AtListItem title="设备编码" extraText={point_info.device_code} />
      <AtListItem
        title="最近报警类型"
        extraText={warnObj[point_info.warn_type]}
      />
      <AtListItem
        title="最近检查状态"
        extraText={inspectStateObj[point_info.inspect_state]}
      />
      <AtListItem
        title="最近检查措施"
        extraText={inspectMeasureObj[point_info.inspect_measure]}
      />
      <AtListItem
        title="最近检查时间"
        extraText={
          point_info.inspect_timestamp
            ? formatTime2(point_info.inspect_timestamp * 1000)
            : "无"
        }
      />
      <AtListItem
        title="最近探测时间"
        extraText={
          point_info.detect_timestamp
            ? formatTime2(point_info.detect_timestamp * 1000)
            : "无"
        }
      />
      <AtListItem title="经度" extraText={point_info.latitude / 1000000} />
      <AtListItem title="纬度" extraText={point_info.longitude / 1000000} />
      <AtListItem title="布点标签" extraText={point_info.tag || "无"} />
    </View>
  );
};

export default Index;
